<?php
/**
  * @desc basic container,static method class
  *
  *
  */
class NICED_BasicContainer
{
	const NAME_SEPARATOR = '.';

    public static function &set(&$v, $name, &$value)
    {
        $b   = self::resolveName($name);
        $ct  = count($b);
        $end = $ct -1;

        for($i = 0; $i < $ct; $i++)
        {
            $n = $b[$i];

            if($i == $end) 
			{
				return $v[$n] = &$value;
			}

            if(!isset($v[$n])) 
            {
                $v[$n] = array();
            }
            else if($v[$n] instanceof NICED_Container) 
            {
                return $v[$n]->setByRef(implode(self::NAME_SEPARATOR, array_slice($b, $i + 1)), $value);
            }
            elseif(!is_array($v[$n])) 
            {
                $v[$n] = array();
            }

            $v = &$v[$n];
        }

		$f = false;

        return $f;
    }

	public static function &get(&$v, $name)
    {
        //NICED_PRINT($name);
        $b    = self::resolveName($name);
        $end  = count($b) - 1;
		$null = NULL;

        for($i = 0; $i <= $end; $i++)
        {
            $n = $b[$i];

            if(!isset($v[$n])) 
			{
				return $null;
			}

            if($i == $end) 
			{
				return $v[$n];
			}

            if($v[$n] instanceof NICED_Container) 
            {
                $v = $v[$n]->get(implode(self::NAME_SEPARATOR, array_slice($b, $i + 1)));
                return $v;
            }

            if(!is_array($v[$n])) 
			{
				return $null;
			}

            $v = &$v[$n];
        } //end for

        return $null;
    }
    
    public static function append(&$v, $name, $value)
    {
        $b   = self::resolveName($name);
        $ct  = count($b);
        $end = $ct -1;

        for($i = 0; $i < $ct; $i++)
        {
            $n = $b[$i];
            if(!isset($v[$n]) && $i != $end) 
            {
                $v[$n] = array();
                $v = &$v[$n];
                continue;
            }
            else if(!isset($v[$n])) 
            {
                $v[$n] = array();
            } 
            else if($v[$n] instanceof NICED_Container) 
            {
                return $v[$n]->append(implode(self::NAME_SEPARATOR, array_slice($b, $i + 1)), $value);
            }
            else if(is_string($v[$n])) 
            {
                return $v[$n] .= $value;
            }
            else if(!is_array($v[$n])) 
            {
                $v[$n] = array();
            } 
            else if(is_array($v[$n]) && $i != $end) 
            {
                $v = &$v[$n];
                continue;
            }

            if($i == $end) 
			{
				return $v[$n][] = &$value;
			}

            if(!($v[$n])) 
			{
				$v[$n][] = array();
			}

            $max = max(array_keys($v[$n]));

            if(!is_numeric($max)) 
			{
				$max = 0;
			}

            $v = &$v[$n][$max];
        } //end for

        return false;
    }

	public static function remove(&$v, $name)
    {
        $b   = self::resolveName($name);
        $end = count($b) -1;

        for($i = 0; $i <= $end; $i++)
        {
            $n = $b[$i];
            if(!isset($v[$n])) 
			{
				return null;
			}

            if($i == $end) 
            {
                $value = &$v[$n];
                unset($v[$n]);
                return $value;
                
            }

            if($v[$n] instanceof NICED_Container) 
            {
                return $v[$n]->remove(implode(self::NAME_SEPARATOR, array_slice($b, $i+1)));
            }

            if(!is_array($v[$n])) 
			{
				return null;
			}

            $v = &$v[$n];
        }

        return null;
    }

    public static function &move(&$v, $target, $destination)
    {
        $r = &self::set($v, $destination, self::get($v, $target));
        self::remove($v, $target);

        return $r;
    }
    
        
     public static function exists(&$v, $name)
     {
        $b   = self::resolveName($name);
        $end = count($b) - 1;

        for($i = 0; $i <= $end; $i++)
        {
            $n = $b[$i];

            if(!array_key_exists($n, $v)) 
			{
				return false;
			}

            if($i == $end) 
			{
				return true;
			}

            if($v[$n] instanceof NICED_Container) 
            {
                return $v[$n]->exists(implode(self::NAME_SEPARATOR, array_slice($b, $i + 1)));
            }

            if(!is_array($v[$n])) 
			{
				return false;
			}

            $v = &$v[$n];
        } //end for

        return false;
    }
       
    public static function isEmpty(&$v, $name)
     {
        $value = self::get($v, $name);

        return empty($value);
	 }

    public static function getNames(&$v, $name = "")
    {
        if(strlen($name)== 0) 
		{
			return array_keys($v);
		}

        if(!self::exists($v, $name)) 
		{
			return array();
		}

        $i = self::get($v, $name);

        if (is_array($i)) 
		{
			return array_keys($i);
		}

        if($i instanceof NICED_Container) 
		{
			return $i->getNames();
		}

        return array();
    }
    

    public static function size(&$v, $name = "")
    {
        if (strlen($name) == 0) 
		{
			return count($v);
		}

        if(!self::exists($v, $name)) 
		{
			return false;
		}

        $i = self::get($v, $name);

        if (is_array($i)) 
		{
			return count($i);
		}

        if(is_numeric($i)) 
		{
			return intval($i);
		}

        if(is_string($i)) 
		{
			return strlen($i);
		}

        if($i instanceof NICED_Container) 
		{
			return $i->size();
		}

        return 0;
    }
    

    public static function &getAllData(&$v = null)
    {
        return $v;
    }

	public static function resolveName($name)
	{
		return explode(self::NAME_SEPARATOR, $name);
	}
}

class NICED_Container
{
    protected $_data = array();
    
    public function __construct($data = null)
    {
        if(is_array($data)) 
		{
            $this->_data = $data;
		}
        
        if($data instanceof NICED_Container) 
		{
            $this->_data = $data->getAllData();
		}    
    }
   

	public function __call($method, $args)
	{
		$args[0] = isset($args[0]) ? $args[0] : $args[0] = '';
		$args[1] = isset($args[1]) ? $args[1] : $args[1] = '';

		return NICED_BasicContainer::$method(self::getAllData(), $args[0], $args[1]);
	}

	public function get($k)
	{
		return NICED_BasicContainer::get(self::getAllData(), $k);
	}

	public function set($k, $v)
	{
		return NICED_BasicContainer::set(self::getAllData(), $k, $v);
	}

    public function &getAllData(&$v = null)
    {
        return $this->_data;
    }

    public function &setByRef($name, &$value)
     {
         $v = &NICED_BasicContainer::set(self::getAllData(), $name, $value);

         return $v;
     }
    
} //end class

/** 
  *
  * @desc singleton container
  */
class NICED_SingletonContainer
{
	public static $vars = array();

	public function __call($method, $args)
	{
		$args[0] = isset($args[0]) ? $args[0] : $args[0] = '';
		$args[1] = isset($args[1]) ? $args[1] : $args[1] = '';

		return NICED_BasicContainer::$method(self::getAllData(), $args[0], $args[1]);
	}

	public static function get($k)
	{
		return NICED_BasicContainer::get(self::getAllData(), $k);
	}

	public static function set($k, $v)
	{
		return NICED_BasicContainer::set(self::getAllData(), $k, $v);
	}

	public static function isEmpty($k)
	{
		return NICED_BasicContainer::isEmpty(self::getAllData(), $k);
	}

    public static function &getAllData(&$v = null)
    {     
        return self::$vars;
    }    
} //end class

class SC extends NICED_SingletonContainer
{

}

class LC extends NICED_SingletonContainer
{
	//public static function set($k, $v)
	//{
    //    parent::set($k, $v);

    //    
    //    $slice = NICED_BasicContainer::resolveName($k);

    //    if (("server" != $slice[0]) && ("dsn" != $slice[0]))
    //    {
    //        return true;
    //    }
    //    else
    //    {
    //        NICED_DBFactory::resolve(true);
    //    }
	//}
}

class NICED_SmartContainer 
{
	
	protected $_data = array();

	public function get($k) 
	{	
		if(!$k) 
		{
			return $this->_data;
		}
		
		$p = str_replace('.', "']['", $k);
		
		return @eval("return \$this->_data['$p'];");
	}
	
	public function set($k, $v) 
	{
		
		$p = str_replace('.', "']['", $k);
		eval ("\$this->_data['$p'] = \$v;");
		
		return $v;
	}

	public function __call($name, $args) 
	{
		
	}
	
	public function __set($k, $v) 
	{
		
	}
	
	public function __get($k) 
	{
		
	}
	
	public function __isset($k) 
	{
		
	}
	
	public function __unset($k)
	{
		
	}

}//end class

?>
